<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<link rel="stylesheet" href="css/popup.css" type="text/css">
	<script src="scripts/jquery-1.5.2.min.js" type="text/javascript"></script>	
	<script type="text/javascript">
	var bkP = chrome.extension.getBackgroundPage().FGS;

	function listDb()
	{
		bkP.database.db.readTransaction(function(tx)
		{
			var arr = {};

			tx.executeSql("SELECT * FROM sqlite_master where type = 'table'", [], function(tx, res)
			{
				$('body').prepend('<table id="totals" style="margin: 0 auto; margin-top: 15px;" width="75%"><caption>'+bkP.getMsg('DatabaseUsageForAllGames')+'</caption><thead><tr><th scope="col" class="first"></th><th scope="col">'+bkP.getMsg('DatabaseName')+'</th><th scope="col">'+bkP.getMsg('DatabaseCount')+'</th><th scope="col" class="last">'+bkP.getMsg('Actions')+'</th></tr></thead><tbody></tbody></table>');				
				
				
				var cnt = 0;
				for(var i = 0; i < res.rows.length; i++)
				{
					var re = new RegExp('(^__|sqlite|options)', "i");
					var r = res.rows.item(i);
					if(!re.test(r.name))
					{
						cnt++;
						arr[r.name] = cnt;

					}					
				}
				var str = [];
				
				var tables = [];
				
				for(var name in arr)
				{
					var v = arr[name];					
					str.push(" SELECT '"+name+"' as name, COUNT(*) as count FROM "+name+" ");
				}
				
				
				tx.executeSql(str.join(" UNION "), [], function(tx, res)
				{
					for(var i = 0; i < res.rows.length; i++)
					{
						$('#totals > tbody').append('<tr><td class="first">'+(i+1)+'.</td><td>'+res.rows.item(i).name+'</td><td>'+res.rows.item(i).count+'</td><td class="last"><a href="#" table="'+res.rows.item(i).name+'" class="emptyDB">Empty</a></td></tr>');
					}
				}, bkP.database.onSuccess, bkP.database.onError);
				
				
				var data = {};
				
				for(var name in arr)
				{
					data[name] = {};
					
					for(var idd in bkP.options.games)
					{
						data[name][idd] = 0;
					}
				}
				
				for(var name in arr)
				{
					tx.executeSql('SELECT "'+name+'" as  name, gameID, COUNT(1) as count FROM '+name+' group by gameID', [], function(tx, res)
					{
						for(var i = 0; i < res.rows.length; i++)
						{
							data[res.rows.item(i).name][res.rows.item(i).gameID] = res.rows.item(i).count;
							
							if(i == (res.rows.length-1))
							{
								listDb2(res.rows.item(i).name, data[res.rows.item(i).name]);
							}							
						}
						if(res.rows.length == 0)
						{
							listDb2(name, data[name]);
						}
						
					}, bkP.database.onSuccess, bkP.database.onError);
				}
			}, bkP.database.onSuccess, bkP.database.onError);
		});
	}
	
	function listDb2(name, data)
	{
		
		for(var idd in data)
		{
			if(typeof bkP.gamesData[idd] == 'undefined')
				continue;
			
			if($('#'+idd).length == 0)	
				$('body').append('<table id="'+idd+'" style="margin: 0 auto; margin-top: 15px;" width="75%"><caption>'+bkP.getMsg('DatabaseUsageForGame')+' '+bkP.gamesData[idd].name+'</caption><thead><tr><th scope="col" class="first"></th><th scope="col">'+bkP.getMsg('DatabaseName')+'</th><th scope="col">'+bkP.getMsg('DatabaseCount')+'</th><th scope="col" class="last">'+bkP.getMsg('Actions')+'</th></tr></thead><tbody></tbody></table>');
			
			
			var cnt = $('#'+idd).children('tbody').children('tr').length+1;
			
			$('#'+idd+' > tbody').append('<tr><td class="first">'+cnt+'.</td><td>'+name+'</td><td>'+data[idd]+'</td><td class="last"><a href="#" table="'+name+'" gameID="'+idd+'" class="emptyDB">Empty</a></td></tr>');
		}
	}
	
	$(function()
	{
		listDb();
		
		$('.emptyDB').live('click',function()
		{
			var gameID = $(this).attr('gameID');
			var table = $(this).attr('table');
			
			if(typeof(gameID) == "undefined")
			{
				bkP.database.db.transaction(function(tx)
				{
					tx.executeSql('DELETE FROM '+table, [], bkP.database.onSuccess, bkP.database.onError);
					bkP.newElements = 0;
					bkP.updateIcon();
					$('body').html('');
					listDb();
				});
			}
			else
			{
				bkP.database.db.transaction(function(tx)
				{
					tx.executeSql('DELETE FROM '+table+' where gameID = ?', [gameID], bkP.database.onSuccess, bkP.database.onError);
					bkP.newElements = 0;
					bkP.updateIcon();
					$('body').html('');
					listDb();
				});
			}
			
		});
	});
	</script>
	<style>

body
{
	padding-bottom: 15px;
}
	table {
border-bottom: 3px solid #B2B2B2;
margin: 0px 0px 2em;
}
caption {
background: #EEE;
border: solid #B2B2B2;
border-width: 1px 0px 2px;
font: normal normal normal 1.1em/normal Georgia, Times, serif;
padding: 0.25em;
}

thead th {
text-align: left;
text-transform: uppercase;
}
th, td {
border: solid #CCC;
border-width: 0px 1px 1px;
padding: 0.25em 0.5em;
}
</style>

</head>
<body>
</body>
</html>